package cn.com.tianpeidong.restaurantadmin.common.util.file;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
  * @author tianpeidong
  * @version 创建时间: 19-3-20 上午11:41
 */
@Component
public class UploadUtil {

    private static String baseUrl;

    private final static Logger LOGGER = LogManager.getLogger();

    private static String endpoint;
    private static String accessKeyId;
    private static String accessKeySecret;


    private static String bucketName;
    private static String key;
//    private static String uploadFile = "e:/plane.png";

    @Value("${ali.oss.key}")
    public void setKey(String key) {
        UploadUtil.key = key;
    }

    @Value("${ali.oss.bucketName}")
    public void setBucketName(String bucketName)  {
        UploadUtil.bucketName = bucketName;
    }

    @Value("${ali.oss.endPoint}")
    public void setEndpoint(String endpoint) {
        UploadUtil.endpoint = endpoint;
    }

    @Value("${ali.accessKey}")
    public void setAccessKeyId(String accessKeyId) {
        UploadUtil.accessKeyId = accessKeyId;
    }

    @Value("${ali.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        UploadUtil.accessKeySecret = accessKeySecret;
    }

    @Value("${ali.oss.baseUrl}")
    public void setBaseUrl(String baseUrl) {
        UploadUtil.baseUrl = baseUrl;
    }


    public static String upload(String filename, String filePath) {

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        try {
            UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key + filename);
            // 待上传的本地文件
            uploadFileRequest.setUploadFile(filePath);
            // 设置并发下载数，默认1
            uploadFileRequest.setTaskNum(5);
            // 设置分片大小，默认100KB
            uploadFileRequest.setPartSize(1024 * 1024 * 1);
            // 开启断点续传，默认关闭
            uploadFileRequest.setEnableCheckpoint(true);

            UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);

            CompleteMultipartUploadResult multipartUploadResult =
                    uploadResult.getMultipartUploadResult();
            // System.out.println(multipartUploadResult.getETag());

            return baseUrl + filename;

        } catch (OSSException oe) {
            LOGGER.error("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            LOGGER.error("Error Message: " + oe.getErrorCode());
            LOGGER.error("Error Code:       " + oe.getErrorCode());
            LOGGER.error("Request ID:      " + oe.getRequestId());
            LOGGER.error("Host ID:           " + oe.getHostId());
        } catch (ClientException ce) {
            LOGGER.error("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            LOGGER.error("Error Message: " + ce.getMessage(), ce);
        } catch (Throwable e) {
            LOGGER.error("未知错误", e);
        } finally {
            ossClient.shutdown();
        }
        return null;
    }
}
